import { list } from "../../api/member";
Page({

    data: {
        seller: [],
        name: '',
        id: null,
        timer: null,
        query: {
            type: 1,
            keyWord: '',
            pageNum: 1,
            pageSize: 20,
        }
    },

    onShow() {
        this.data.query.pageNum = 1
        this.data.query.pageSize = 20
        this.setData({
            seller: [],
            [`query.pageNum`]: this.data.query.pageNum,
            [`query.pageSize`]: this.data.query.pageSize,
        })
        this.search()
    },
    onPullDownRefresh() {
        wx.showLoading({
            title: '加载中',
        })
        this.data.seller = []
        this.data.query.pageNum = 1
        this.setData({
            [`query.pageNum`]: this.data.query.pageNum,
            seller: this.data.seller
        })
        setTimeout(() => {
            this.search(() => {
                wx.hideLoading()
                wx.stopPullDownRefresh()
            })
        }, 1000)
    },
    onReachBottom() {
        this.data.query.pageNum += 1
        this.setData({
            [`query.pageNum`]: this.data.query.pageNum
        })
        this.search()
    },

    search(callBack) {
        list(this.data.query).then(res => {
            console.log(res);
            this.setData({
                seller: res.data.data
            })
        })
        //如果回调函数存在，则执行该回调函数。
        callBack && callBack()
    },

    add() {
        wx.navigateTo({
            url: '/pages/pagesUser/companyInfo/attestation?sellearAdd=' + true,
        })
    },

    selectSeller(e) {
        if (e.currentTarget.dataset.status !== 0 && e.currentTarget.dataset.status !== 2) {
            this.setData({
                name: e.currentTarget.dataset.name,
                id: e.currentTarget.dataset.id
            })
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
                memberId: e.currentTarget.dataset.id,
                sellerName: e.currentTarget.dataset.name,
                linkman: e.currentTarget.dataset.linkman,
            })
            wx.navigateBack({
                delta: 1
            })
        } else {
            if (e.currentTarget.dataset.status === 0) {
                wx.showToast({
                    title: '未认证不能选择',
                    icon: 'error'
                })
            }
            if (e.currentTarget.dataset.status === 2) {
                wx.showToast({
                    title: '已驳回不能选择',
                    icon: 'error'
                })
            }
            return false
        }
    },
    keyword(e) {
        console.log(e.detail.value);
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            this.data.query.keyWord = e.detail.value;
            this.search();
        }, 500)
    },
    
})
